package com.tianyang.yukuaifu.business.main.presenter;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.tianyang.yukuaifu.business.main.bean.BrowserInput;
import com.tianyang.yukuaifu.business.main.contract.BrowserX5Contract;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.module.dialog.MaterialDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifu.utils.DownloadImgUtils;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.permissions.PermissionsUtils;

import java.util.HashMap;
import java.util.Map;

import rx.subscriptions.CompositeSubscription;

/**
 * Description：集成Tencent X5
 */

public class BrowserX5Presenter implements BrowserX5Contract.Presenter {

    private BrowserX5Contract.View mView;

    private MaterialDialog mDialog = null;

    private CompositeSubscription mSubscription;

    public BrowserX5Presenter(BrowserX5Contract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initViewData(BrowserInput mInput) {
        if (mInput != null) {
            mView.showTitleLayout(mInput.getTitle(), mInput.isShowTitle());
            if (mInput.isShowPreLoad()) {
                mView.showPreLoading();
            }
            if (!TextUtils.isEmpty(mInput.getReferrer())) {
                Map<String, String> extraHeaders = new HashMap<>();
                extraHeaders.put(MainConfig.INTENT_REFERER, mInput.getReferrer());
                mView.loadUrl(mInput.getUrl(), extraHeaders);
            } else {
                if (!TextUtils.isEmpty(mInput.getUrl())) {
                    mView.loadUrl(mInput.getUrl());
                } else {
                    mView.loadDataWithBaseURL(mInput.getContent());
                }
            }
        }
    }

    @Override
    public void saveImageFile(final Bitmap bitmap) {
        if (mDialog == null) {
            mDialog = new MaterialDialog(mView.getContext());
            mDialog.title("提示").content("保存这个分享图片吗？").btnText("确定", "取消").show();
            mDialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    if (!PermissionsUtils.checkWriteStoragePermission(mView.getContext()))
                        return;
                    boolean isSuccess = DownloadImgUtils.saveBitmap(mView.getContext(), bitmap);
                    ToastUtil.showTextViewPrompt(isSuccess? "图片保存成功!请在相册查看" : "图片保存失败");
                    mDialog.dismiss();
                }
            }, new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    mDialog.dismiss();
                }
            });
        } else {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
            }
        }
        mDialog.show();
    }

    @Override
    public void onDestroyPresenter() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
